package org.jiuling.seo.tools.baidu;

import org.jiuling.local.tools.convert.StringConvert;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.util.RegularUtil;
import org.jiuling.local.tools.verify.ListVerify;
import org.jiuling.local.tools.verify.StringVerify;
import org.jiuling.network.tools.http.request.exception.RequestException;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 百度收录
 * @author 紫灵
 * @date 2022年02月23日 17时18分09秒
 */
public class BaiDuRecord{

    /**
     * 获取百度索引；量
     * @param url url地址
     * @param fullUrl 完整的百度查询地址
     * @return 索引量
     */
    public static int getIndexQuantity(String url,String fullUrl,Proxy proxy) throws IOException, RequestException, LocalToolsException {
        if(StringVerify.isBlank(fullUrl)){
            url = StringConvert.replace(url,"","http://","https://");
            fullUrl = "http://www.baidu.com/s?wd=site:"+url;
        }
//        String baiduHtml = HttpRequest.send(new URL(fullUrl),  proxy, RequestMethod.GET,null,null,5000,5000);
        String baiduHtml = null;
        List<String> regularList = RegularUtil.getValue("找到相关结果数约(.*?)个",baiduHtml);
        if(ListVerify.isEmpty(regularList)){
            regularList = RegularUtil.getValue(">百度为您找到相关结果(.*?)个",baiduHtml);
            if(ListVerify.isEmpty(regularList)) {
                fullUrl = RegularUtil.getValue("window.location.href = \"(.*?)\";", baiduHtml).get(0);
                try {
                    // 如果没有直接取到值就暂停4秒之后获取到重定向的值再去请求
                    Thread.sleep(4000);
                } catch (InterruptedException ignored) {
                }
                return getIndexQuantity(url, fullUrl,proxy);
            }else {
                return Integer.parseInt(regularList.get(0));
            }
        }else {
            return Integer.parseInt(regularList.get(0));
        }
    }

    public static void main(String[] args) throws IOException, RequestException, InterruptedException, LocalToolsException {
        String url = "tools.jiulingcloud.cn";
        Map<String,Integer> map = new HashMap<>();
        map.put("110.230.223.171",4231);
        map.put("182.204.180.23",4231);
        map.put("175.174.183.22",4251);
        map.put("114.106.151.118",4286);
        map.put("27.158.126.91",4245);
        map.put("42.177.136.247",4213);
        map.put("1.199.40.149",4254);
        map.put("117.26.40.221",4245);
        map.put("106.110.198.184",4264);
        map.put("58.218.7.51",4231);
        int i= 0;
        while (true){
            i++;
            Thread.sleep(4000);
            //创建代理服务器
            Iterator<Map.Entry<String, Integer>> it = map.entrySet().iterator();
//            System.out.println(it.next().getKey());
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(it.next().getKey(), it.next().getValue()));
            System.out.println(i+" 次，收录量："+getIndexQuantity(url,null,proxy));
        }
    }
}
